﻿using CLVisionIOCardNC620.IOCardDevice;
using NeuroCheck.BaseTypes.DeviceDriver;
using System;
using System.Runtime.CompilerServices;

namespace CLVisionIOCardNC620
{
    // Token: 0x02000002 RID: 2
    //在使用的板卡操作方法
    public class CLVisionIOCardDevice
    {
        //***************Control Device****************
        /// <summary> Names of available methods.
        /// </summary>
        public String[] MethodNames
        {
            get { return MethodNameConstants.Names; }
        }
        // public String myLabel;
        /// <summary> Checks if the method name is a valid string
        /// </summary>
        /// <param name="objName_">Name as object</param>
        /// <param name="strName_">Name as string</param>
        /// <returns>True if valid</returns>
        private Boolean TryGetMethodName(Object objName_, out String strName_)
        {
            strName_ = objName_ as String;
            if (null == strName_)
            {
                return false;
            }
            return true;
        }
        /// <summary> Execute a device method.
        /// </summary>
        /// <param name="objName_">Method name.</param>
        /// <returns>True: No error occured</returns>
        internal Boolean Execute(int CardID,object objName_, object objValue)
        {
            String strName;
            if (false == TryGetMethodName(objName_, out strName))
            {
                return false;
            }
            switch (strName)
            {
                
                case MethodNameConstants.ResetAllOutput: //清除所有的输出
                    return ResetAllOutput(CardID);
                case MethodNameConstants.EnableInterrupt: //使能所有Input点位的中断
                    return EnableInterrupt(CardID);
                case MethodNameConstants.SetAllOutput: //置位所有的输出
                    return SetAllOutput(CardID);
                case MethodNameConstants.DisableInterrupt://关闭所有Input点位的中断
                    return DisableInterrupt(CardID);

                default:
                    return false;
            }
        }

        /// <summary> Flips the image data vertical.
        /// </summary>
        /// <returns>True: Flipped successfully.</returns>
        private Boolean ResetAllOutput(int nCardID = 0)
        {
            if (!IIOCardDevice.SetOutPutData(nCardID, 0xff, out _, out _))
            {
                return false;
            }
            return true;
        }
        /// <summary> Flips the image data vertical.
        /// </summary>
        /// <returns>True: Flipped successfully.</returns>

        private Boolean SetAllOutput(int nCardID = 0)
        {
            if (!IIOCardDevice.SetOutPutData(nCardID, 0x00, out _, out _))
            {
                return false;
            }
            return true;
        }
        private Boolean DisableInterrupt(int nCardID = 0)
        {
            if (!IIOCardDevice.SetInterruptEnable(nCardID, 0, out _, out _))
            {
                return false;
            }
            return true;
        }
        private Boolean EnableInterrupt(int nCardID = 0)
        {
            if (!IIOCardDevice.SetInterruptEnable(nCardID, 1, out _, out _))
            {
                return false;
            }
            return true;
        }


        /// <summary> Checks if the property name is a valid string
        /// </summary>
        /// <param name="objName_">Name as object</param>
        /// <param name="strName_">Name as string</param>
        /// <returns>True if valid</returns>
        private Boolean TryGetPropertyName(Object objName_, out String strName_)
        {
            strName_ = objName_ as String;
            if (null == strName_)
            {
                return false;
            }
            return true;
        }
        /// <summary> Get the type of a property.
        /// </summary>
        /// <param name="objName_">Property name.</param>
        /// <param name="type_">Property type.</param>
        /// <returns>True: No error occured</returns>
        internal Boolean GetPropertyType(object objName_, out object type_)
        {
            String strName;
            if (false == TryGetPropertyName(objName_, out strName))
            {
                type_ = null;
                return false;
            }

            switch (strName)
            {
                case PropertyNameConstants.InputBit:
                    type_ = NcDevicePropertyTypeInfo.Int;
                    return true;
                case PropertyNameConstants.OutputBit:
                    type_ = NcDevicePropertyTypeInfo.Int;
                    return true;
                default:
                    type_ = null;
                    return false;
            }
        }
     
      

        /// <summary> Get the possible values of a property.
        /// </summary>
        /// <param name="objName_">Property name.</param>
        /// <param name="values_">Possible property values.</param>
        /// <returns>True: No error occured</returns>
        internal Boolean GetPropertyPossibleValues(object objName_, out object values_)
        {
            String strName;
            if (false == TryGetPropertyName(objName_, out strName))
            {
                values_ = null;
                return false;
            }

            switch (strName)
            {
                case PropertyNameConstants.InputBit:
                    {
                        long InputData = 0;
                        IIOCardDevice.GetInPutData(this.m_TempDeviceId, out InputData, out _, out _);
                        values_ = (int)InputData;
                    }
                    return true;
                case PropertyNameConstants.OutputBit:
                    {
                        long OutPutData = 0;
                        IIOCardDevice.GetOutPutData(this.m_TempDeviceId, out OutPutData, out _, out _);
                        values_ = (int)OutPutData;
                    }
                    return true;

                default:
                    values_ = null;
                    return false;
            }
        }

        /// <summary> Get the current value of a property.
        /// </summary>
        /// <param name="objName_">Property name.</param>
        /// <param name="value_">Property value.</param>
        /// <returns>True: No error occured</returns>
        internal Boolean GetPropertyValue(object objName_, out object value_)
        {
            String strName;
            if (false == TryGetPropertyName(objName_, out strName))
            {
                value_ = null;
                return false;
            }

            switch (strName)
            {
                case PropertyNameConstants.InputBit:
                    {
                        long InputData = 0;
                        IIOCardDevice.GetInPutData(this.m_TempDeviceId, out InputData, out _, out _);
                        value_ = (int)InputData;
                    }
                    return true;
                case PropertyNameConstants.OutputBit:
                    {
                        long OutPutData = 0;
                        IIOCardDevice.GetOutPutData(this.m_TempDeviceId, out OutPutData, out _, out _);
                        value_ = (int)OutPutData;

                    }
                    return true;

                default:
                    value_ = null;
                    return false;
            }
        }

        /// <summary> SetPropertyValue to a device.
        /// </summary>
        /// <param name="objName_">Method name.</param>
        /// <returns>True: No error occured</returns>
        internal Boolean SetPropertyValue(object objName_, object objValue)
        {
            String strName;
            if (false == TryGetPropertyName(objName_, out strName))
            {
                return false;
            }
            bool nRet = false;
            switch (strName)
            {
                case PropertyNameConstants.InputBit:
                    return true;
                case PropertyNameConstants.OutputBit:
                    int nValues = (int)objValue;
                    if (nValues > 255)
                        nValues = 255;
                    nRet = IIOCardDevice.SetOutPutData(this.m_TempDeviceId, (long)nValues, out _, out _);
                    return nRet;

                default:
                    return false;
            }
        }
        //THE END*************************************************************
        // Token: 0x06000001 RID: 1 RVA: 0x00002050 File Offset: 0x00000250
        public CLVisionIOCardDevice(int tempDeviceId, IIOCardDevice iIOCardDevice)
        {
            this.m_TempDeviceId = tempDeviceId;
            this.iIOCardDevice = iIOCardDevice;

        }


        private int m_TempDeviceId;
        //使用的板卡ID
        public int TempDeviceId
        {
            get
            {
                return this.m_TempDeviceId;
            }
        }

        private IIOCardDevice iIOCardDevice;
        //使用的板卡对象
        public IIOCardDevice IIOCardDevice
        {
            get
            {
                return this.iIOCardDevice;
            }
        }

        // Token: 0x06000486 RID: 1158 RVA: 0x000219C0 File Offset: 0x0001FBC0
        public int TestDigitize()
        {
            bool Res;
            long InputData = 0;

            Res = IIOCardDevice.GetInPutData(this.TempDeviceId, out InputData, out _, out _);
            if (Res)
            {
                return 1;
            }
            return 0;
        }


        // Token: 0x0600000E RID: 14 RVA: 0x00002402 File Offset: 0x00000602
        public void Dispose()
        {
            IIOCardDevice.CloseIODevice(this.TempDeviceId, out _, out _);
            //关闭板卡，在退出系统前必段执行一次此函数，释放内存。

        }

        //04-22，参照IDS****************************************************************8
        // Token: 0x06000497 RID: 1175 RVA: 0x000221BC File Offset: 0x000203BC
        public void SetParameterToDefault()
        {
            ResetAllOutput();
        }

    }
}
